package com.csun.cmny.provider.device.csun.camera;

import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.netty.NettyInitRunner;
import io.netty.bootstrap.ServerBootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-27 08:57
 * @Description:
 */
@Slf4j
@Order(27)
@Configuration
@ConditionalOnProperty(prefix = "cmny.netty.camera", name = "switch", havingValue = "on")
public class CameraNettyConfig {

    @Resource
    private CmnyProperties cmnyProperties;

    @Resource
    private CameraNettyTcpChannelHandler cameraNettyTcpChannelHandler;

    @Bean
    public CameraNettyTcpServer cameraNettyTcpServer() {

        int port = cmnyProperties.getNetty().getCamera().getPort();

        log.info("###CameraInitRunner starting...<<<<<<<<<<<<<");

        CameraNettyTcpServer cameraNettyTcpServer =
                new CameraNettyTcpServer(port, cameraNettyTcpChannelHandler, new ServerBootstrap());

        cameraNettyTcpServer.init();

        NettyInitRunner.nettyServerMap.put(port, cameraNettyTcpServer);

        log.info("###CameraInitRunner finish<<<<<<<<<<<<<");

        return cameraNettyTcpServer;
    }

}